package com.study.shengsiyuan.example06;

import java.util.Random;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class MyProtobufClientHandler extends SimpleChannelInboundHandler<MyDataInfo.MyMessage> {

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.MyMessage msg) throws Exception {

	}
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		MyDataInfo.MyMessage myMessage=null;
		int random =new Random().nextInt(3);
		if(0==random) {
			myMessage = MyDataInfo.MyMessage.newBuilder()
					.setDataType(MyDataInfo.MyMessage.DataType.PersonType)
					.setPerson(
							MyDataInfo.Person.newBuilder()
							.setName("张三").setAge(21).setAddress("合肥").build()
							).build();
		}else if(1==random) {
			myMessage = MyDataInfo.MyMessage.newBuilder()
					.setDataType(MyDataInfo.MyMessage.DataType.DogType)
					.setDog(
							MyDataInfo.Dog.newBuilder()
							.setName("狗狗").setAge(1).build()
							).build();
		}else if(2==random) {
			myMessage = MyDataInfo.MyMessage.newBuilder()
					.setDataType(MyDataInfo.MyMessage.DataType.CatType)
					.setCat(
							MyDataInfo.Cat.newBuilder()
							.setName("猫咪").setCity("合肥").build()
							).build();
		}
		ctx.channel().writeAndFlush(myMessage);
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		ctx.close();
	}
}
